﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Ryder_US_Website.BUS;
using System.Collections.Generic;

namespace Ryder_US_Website.Presentation.Admin
{
    public partial class ServiceDetail_Update : System.Web.UI.Page
    {
        private RUS_ServiceDetail srd = new RUS_ServiceDetail();
        protected void DropdownlistLoad()
        {
            List<RUS_Service> list = ServiceBUS.GetAll();
            DropDownListTenDichVu.DataSource = list;
            DropDownListTenDichVu.DataTextField = "Service_name";
            DropDownListTenDichVu.DataValueField = "Service_ID";
            DropDownListTenDichVu.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["RUS_User"] == null)
            {
                Response.Redirect("Login.aspx");
            }
            string id = Request.QueryString["id"];
            int temp = Int16.Parse(id);
            srd = ServiceDetailBUS.GetServiceDetail(temp);
            
            if (!IsPostBack)
            {
                DropdownlistLoad();
                txtTenChiTietDV.Text = srd.ServiceDetail_name;
                txtDonViTinh.Text = srd.ServiceDetail_unit;
                txtTrongLuongMin.Text = (srd.ServiceDetail_unitMin).ToString();
                txtTrongLuongMax.Text = (srd.ServiceDetail_unitMax).ToString();
                txtDonGia.Text = (srd.ServiceDetail_price).ToString();
                txtMoTa.Text = srd.ServiceDetail_description;
                rbConSuDung.Checked = (srd.ServiceDetail_trash == 0);
                rbDaXoa.Checked = !rbConSuDung.Checked;
                for (int i = 0; i < DropDownListTenDichVu.Items.Count; i++)
                {
                    if (srd.ServiceDetail_ServiceID.ToString().Equals(DropDownListTenDichVu.Items[i].Value))
                         DropDownListTenDichVu.SelectedIndex = i;
                }
            }
        }

        protected void btnHuy_Click(object sender, EventArgs e)
        {
            Response.Redirect("ServiceDetail_List.aspx");
        }

        protected void btnCapNhat_Click(object sender, EventArgs e)
        {
            RUS_ServiceDetail kq = new RUS_ServiceDetail();
            srd.ServiceDetail_ServiceID = Int16.Parse(DropDownListTenDichVu.SelectedItem.Value);
            srd.ServiceDetail_name = txtTenChiTietDV.Text;
            srd.ServiceDetail_unit = txtDonViTinh.Text;
            srd.ServiceDetail_unitMin = Int16.Parse(txtTrongLuongMin.Text);
            srd.ServiceDetail_unitMax = Int16.Parse(txtTrongLuongMax.Text);
            srd.ServiceDetail_price = Decimal.Parse(txtDonGia.Text);
            srd.ServiceDetail_description = txtMoTa.Text;
            srd.ServiceDetail_trash = (rbConSuDung.Checked == true) ? 0 : 1;
            try
            {
                kq = ServiceDetailBUS.Update(srd.ServiceDetail_ServiceID, srd.ServiceDetail_name, srd.ServiceDetail_unit,
                                             srd.ServiceDetail_unitMin, srd.ServiceDetail_unitMax,
                                             srd.ServiceDetail_price, srd.ServiceDetail_description,
                                             srd.ServiceDetail_trash, srd.ServiceDetail_ID);
            }
            catch (Exception ex)
            {
                Response.Write(Utils.UtilHelpers.ShowMessage(ex.Message));
            }
            if (srd.ServiceDetail_ID.Equals(kq.ServiceDetail_ID))
            {
                Response.Redirect("ServiceDetail_List.aspx");
            }
            else
            {
                Response.Write(Utils.UtilHelpers.ShowMessage("Cập nhật chi tiết dịch vụ thất bại"));
            }
        }

        protected void DropDownListTenDichVu_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }
    }
}
